package com.softkey;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class security extends jsyunew3 {
    private String lock_path;
    private String key_id;
    private String passwd_rx = "00000000";
    private String passwd_ry = "00000000";
    private String passwd_wx = "00000000";
    private String passwd_wy = "00000000";

    public security() {
        super();
        setID();
    }

    public security(int num) {
        super();
        this.lock_path = FindPort(num);
        setID();
    }

    public security(SecurityInfo info, int num) {
        super();
        this.lock_path = FindPort(num);
        this.key_id = info.getKey_id();
        this.passwd_rx = info.getPasswd_rx();
        this.passwd_ry = info.getPasswd_ry();
        this.passwd_wx = info.getPasswd_wx();
        this.passwd_wy = info.getPasswd_wy();
    }

    /* 初始化之前需要先获取 */
    public SecurityInfo init() {
        ReSet(lock_path);/*重置加密狗 默认密码：00000000-00000000*/
        SecurityInfo info = new SecurityInfo();
        /* 存入信息 */
        info.setCache_info(random(32));
        info.setInfo_pos(0);
        info.setInfo_len(WriteString(info.getInfo_pos(), info.getCache_info()));
        /* 设置读取密码 */
        info.setPasswd_rx(random(8));
        info.setPasswd_ry(random(8));
        if (setReadPasswd(info.getPasswd_rx(), info.getPasswd_ry()) != 0) {
            System.out.println("读取密码设置错误");
        }
        /* 设置写入密码 */
        info.setPasswd_wx(random(8));
        info.setPasswd_wy(random(8));
        if (setWritePasswd(info.getPasswd_wx(), info.getPasswd_wy()) != 0) {
            System.out.println("写入密码设置错误");
        }
        /* 设置增强加密密钥 */
        info.setSecretKey(random(32));
        if (setSecretKey(info.getSecretKey()) != 0) {
            System.out.println("增强密码写入失败");
        }
        info.setKey_id(key_id);
        System.out.println(info);
        return info;
    }

    /* 当前锁是否存在 */
    public boolean isExist() {
        return get_LastError() == 0;
    }

    /* 设置锁路径 */
    public void setlock_path(int num) {
        this.lock_path = FindPort(num);
    }

    /* 客户端加密 */
    public String client_Encode(String data, int start, int end) {
        data = encrypt.StrEnc(encrypt.StrEnc(Encode(data), key_id), ReadString(start, end));
        return encrypt.StrEnc(data, data);
    }

    /* 客户端解密 */
    public String client_Decode(String data, int start, int end) {
        return encrypt.StrDec(encrypt.StrDec(encrypt.StrDec(data, data), key_id), ReadString(start, end));
    }

    /* 服务端加密 */
    public String Server_Encode(String data, String cache_info) {
        data = encrypt.StrEnc(encrypt.StrEnc(data, cache_info), key_id);
        return encrypt.StrEnc(data, data);
    }

    /* 服务端解密 */
    public String Server_Decode(String data,String cache_info,String secretKey) {
        return Decode(encrypt.StrDec(encrypt.StrDec(encrypt.StrDec(data,data),cache_info),key_id),secretKey);
    }
    /* ==============================================================================================================*/

    /**
     * 设置加密狗 增强密钥-覆盖性设置
     */
    private int setSecretKey(String key) {
        return SetCal_2(key, lock_path);
    }

    /**
     * 增强加密
     */
    private String Encode(String data) {
        return EncString(data, lock_path);
    }

    /**
     * 增强解密
     */
    private String Decode(String data, String key) {
        return DecString(data, key);
    }

    /**
     * 获取锁ID
     */
    private void setID() {
        this.key_id = hex((int) GetID_1(lock_path)) + hex((int) GetID_2(lock_path));
    }

    /**
     * 转换 16进制
     */
    private String hex(int key) {
        return Integer.toHexString(key).toUpperCase();
    }

    /**
     * 设置读取密码
     */
    private int setReadPasswd(String Hkey, String Lkey) {
        return SetReadPassword(passwd_rx, passwd_ry, Hkey, Lkey, lock_path);
    }

    /**
     * 设置写入密码
     */
    private int setWritePasswd(String Hkey, String Lkey) {
        return SetWritePassword(passwd_wx, passwd_wy, Hkey, Lkey, lock_path);
    }

    /**
     * 写入数据
     */
    private int WriteString(int addr, String data) {
        return (NewWriteString(data, addr, passwd_rx, passwd_ry, lock_path));
    }

    /**
     * 使用带长度的方法从指定的地址读取字符串
     */
    private String ReadString(int start, int end) {
        return NewReadString(start, end, passwd_rx, passwd_ry, lock_path);
    }

    /**
     * 创建随机数
     */
    private String random(int num) {
        String[] charactor = "1.Q.A.Z.2.W.S.X.3.E.D.C.4.R.F.V.5.T.G.B.6.Y.H.N.7.U.J.M.8.I.K.9.O.L.0.P.".split("\\.");
        StringBuffer sb = new StringBuffer();
        while (num-- > 0)
            sb.append(charactor[(int) (charactor.length * Math.random())]);
        return sb.toString();
    }

}
